package com.example.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.study.entity.LoginUser;
import com.example.study.mapper.MenuMapper;
import com.example.study.mapper.UserMapper;
import com.example.study.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
* 实现自己数据库查询判断
* 返回UserDetails的实现类
*
* */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("account", s);
        User user = userMapper.selectOne(userQueryWrapper);
        if(user==null){
            throw new RuntimeException("用户账号或者密码错误");
        }
        //用户全信息Collection
        List<String> list = menuMapper.selectPermism(user.getAccount());
        //查询权限，存入LoginUser 权限信息时Collection类型
        return new LoginUser(user,list);
    }
}
